<template>
    <div class="home">
      <!-- 头部导航 -->
      <van-nav-bar
        title="标题"
        class="page-nav-bar"
        fixed
      >
        <template #title>
          <van-button to="/search" class="search-btn" icon="search" type="primary">搜索</van-button>
        </template>
      </van-nav-bar>

      <!-- 滑动导航 -->
      <van-tabs sticky offset-top="1.22rem" class="home-tab" v-model="active" animated swipeable>
        <van-tab v-for="item in channels" :key="item.id" :title="item.name">
          <article-list :channelId="item.id" />
        </van-tab>
        <template #nav-right>
          <div class="placeholder-box"></div>
          <div class="right-menu" @click="isShowEditPopup = true">
            <i class="toutiao toutiao-gengduo"></i>
          </div>
        </template>
      </van-tabs>

      <!-- 编辑频道弹层 -->
      <van-popup
        closeable
        close-icon-position="top-left"
        v-model="isShowEditPopup"
        position="bottom"
        :style="{ height: '90%' }"
        class="editPopup"
      >
        <channel-edit :channels="channels" :active="active" @set-active="active--" @on-go="onGo"/>
      </van-popup>
    </div>
</template>

<script>
import { getUserSelfChannels } from '@/api/user.js'
import { Toast } from 'vant'
import ArticleList from './components/ArticleList.vue'
import ChannelEdit from './components/ChannelEdit.vue'
import { getStorage } from '@/utils/storage.js'
export default {
  components: { ArticleList, ChannelEdit },
  data () {
    return {
      active: 0,
      channels: [],
      isShowEditPopup: false
    }
  },

  created () {
    this.loadUserChannels()
  },

  methods: {
    // 加载用户频道列表
    async loadUserChannels () {
      try {
        // 采用本地数据
        // 未登录 并且 本地有数据 才会使用本地数据
        const storageData = getStorage('MY-CHANNELS')
        if (!this.$store.state.user && storageData) {
          this.channels = storageData
          return false
        }

        const res = await getUserSelfChannels()
        // console.log(res)
        this.channels = res.channels
      } catch (err) {
        Toast.fail('获取用户频道列表失败')
      }
    },
    onGo (e) {
      this.active = e
      this.isShowEditPopup = false // 关闭弹层
    }
  }
}
</script>

<style scoped lang="less">
.home {
  padding-top: 100px;
  // 头部导航
  .search-btn {
    width: 555px;
    height: 64px;
    background: rgba(255, 255, 255, .2);
    border: none;
    border-radius: 32px;
  }
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .van-icon-search {
    color: #fff;
  }

  // 滑动导航
  /deep/.home-tab {
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      height: 82px;
      .van-tab__text {
        font-size: 28px;
        color: #777;
      }
    }

    .van-tabs__wrap {
      height: 82px;
      border-bottom: 1px solid #edeff3;
    }

    .van-tab--active {
      .van-tab__text {
        font-size: 30px;
        color: #333;
      }
    }

    .van-tabs__line {
      width: 31px;
      height: 6px;
      background: #3296FA;
      border-radius: 3px;
      bottom: 8px;
    }

    .van-tabs__nav--line {
      padding-bottom: 0;
    }

    .right-menu {
      width: 66px;
      height: 82px;
      background-color: #fff;
      // flex-shrink: 0;
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      i {
        font-size: 40px;
      }
      &::before {
        content: '';
        width: 2px;
        height: 58px;
        position: absolute;
        left: 0;
        top: 12px;
        background-image: url(~@/assets/images/gradient-gray-line.png);
        background-size: 100% 100%;
      }
    }

    .placeholder-box {
      width: 66px;
      flex-shrink: 0;
    }
  }

  // 频道弹层
  .editPopup {
    padding: 100px 20px 0 20px;
    box-sizing: border-box;
  }
}
</style>
